#-*- coding: utf-8 -*-
from django.shortcuts import render_to_response
from django.shortcuts import render
from django.http import HttpResponse
from blog.models import BlogContent
from datetime import datetime
from django.http import Http404
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

def index(request):
    posts = BlogContent.objects.all()
    paginator = Paginator(posts, 5)
    page = request.GET.get('page')
    try:
        post_list = paginator.page(page)
    except PageNotAnInteger:
        post_list = paginator.page(1)
    except EmptyPage:
        post_list = paginator.paginator(paginator.num_pages)
    return render(request, 'home.html', {'post_list':post_list})
	
def about(req):
	return render(req, 'about.html')

def detail(request, id):
    try:
        post = BlogContent.objects.get(id=str(id))
    except BlogContent.DoesNotExist:
        raise Http404
    return render(request, 'post.html', {'post' : post})

def search_tag(req, tag):
	try:
		post_list = BlogContent.objects.filter(label__iexact = tag)
	except BlogContent.DoesNotExist:
		raise Http404
	return render(req, 'tag.html', {'post_list':post_list})

def archives(req) :
    try:
        post_list = BlogContent.objects.all()
    except BlogContent.DoesNotExist :
        raise Http404
    return render(req, 'archives.html', {'post_list' : post_list, 'error':False})